package com.csxy.six.controller;

import com.csxy.six.R;
import com.csxy.six.entity.GoodsFavorite;
import com.csxy.six.entity.vo.GoodsFavoriteVO;
import com.csxy.six.service.GoodsFavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Tag(name = "goods-favorites", description = "商品收藏管理")
@RestController
@RequestMapping("/market/favorites")
public class GoodsFavoriteController {

    @Autowired
    private GoodsFavoriteService goodsFavoriteService;

    @Operation(summary = "新增商品收藏", description = "新增商品收藏")
    @PostMapping("/save")
    public R addFavorite(@RequestBody GoodsFavoriteVO vo) {
        GoodsFavorite favorite = new GoodsFavorite();
        favorite.setGoodsId(vo.getGoodsId());
        favorite.setUserId(vo.getUserId());

        boolean flag = goodsFavoriteService.getBaseMapper().insert(favorite) > 0;
        if (flag) {
            return R.ok();
        }
        return R.error();
    }


    @Operation(summary = "根据用户ID和商品ID移除商品收藏", description = "根据用户ID和商品ID移除商品收藏")
    @DeleteMapping("/remove")
    public R removeFavorite(
            @Parameter(name = "userId", description = "用户ID", required = true)
            @RequestParam Integer userId,
            @Parameter(name = "goodsId", description = "商品ID", required = true)
            @RequestParam Integer goodsId) {
        boolean flag = goodsFavoriteService.removeFavorite(userId, goodsId);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @Operation(summary = "根据用户ID查询所有收藏商品", description = "根据用户ID查询所有收藏商品")
    @GetMapping("/list/{userId}")
    public R getFavoritesByUserId(@PathVariable Integer userId) {
        List<GoodsFavorite> list = goodsFavoriteService.getFavoritesByUserId(userId);
        return R.ok().data("items", list);
    }
}
